package org.asiainfo.file.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.file.service.AttactmentService;
import org.asiainfo.file.domain.vo.AttactmentVo;
import org.asiainfo.file.domain.bo.AttactmentBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 附件信息
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attactment")
public class AttactmentController extends BaseController {

    private final AttactmentService attactmentService;

    /**
     * 分页查询
     *
     * @param attactmentBo 筛选条件
     * @param pageQuery    分页对象
     * @return 查询结果
     */
    @SaCheckPermission("file:attactment:list")
    @GetMapping("/list")
    public TableDataInfo<AttactmentVo> list(AttactmentBo attactmentBo, PageQuery pageQuery) {
        return attactmentService.queryPageList(attactmentBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param attactId 主键
     * @return 单条数据
     */
    @SaCheckPermission("file:attactment:query")
    @GetMapping("/{attactId}")
    public R<AttactmentVo> getInfo(@NotNull(message = "附件ID不能为空") @PathVariable("attactId") Long attactId) {
        return R.ok(attactmentService.queryById(attactId));
    }

    /**
     * 新增数据
     *
     * @param attactmentBo 附件信息
     * @return 新增结果
     */
    @SaCheckPermission("file:attactment:add")
    @Log(title = "附件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AttactmentBo attactmentBo) {
        if (attactmentService.checkAttactmentApplyNameUnique(attactmentBo)) {
            return R.fail("新增附件文件" + attactmentBo.getApplyName() + "失败，业务名称已存在");
        }
        return toAjax(attactmentService.insertByBo(attactmentBo));
    }

    /**
     * 编辑数据
     *
     * @param attactmentBo 附件信息
     * @return 编辑结果
     */
    @SaCheckPermission("file:attactment:edit")
    @Log(title = "附件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AttactmentBo attactmentBo) {
        if (attactmentService.checkAttactmentApplyNameUnique(attactmentBo)) {
            return R.fail("新增附件文件" + attactmentBo.getApplyName() + "失败，业务名称已存在");
        }
        return toAjax(attactmentService.updateByBo(attactmentBo));
    }

    /**
     * 删除数据
     *
     * @param attactIds 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("file:attactment:remove")
    @Log(title = "附件信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attactIds}")
    public R<Void> remove(@NotNull(message = "附件ID不能为空") @PathVariable Long[] attactIds) {
        return toAjax(attactmentService.deleteWithValidByIds(List.of(attactIds)));
    }
}

